﻿
function extraInit()
{
    initTree(false, false, false);
}


function selectMonth()
{
    var month = parseInt($("input[name=tbxMonth]").val());
    var year = parseInt($("input[name=tbxYear]").val());
    if (isNaN(month) || isNaN(year))
    {
        var date = new Date();
        month = date.getMonth();
        year = date.getFullYear();
    }
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function previousMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month--;
    if (month < 1)
    {
        year--;
        month = 12;
    }
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function nextMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month++;
    if (month > 12)
    {
        year++;
        month = 1;
    }
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function previousYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year--;
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function nextYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year++;
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function selectTreeNode(jobTitleId)
{
    //$("input[name=jobTitleId]").val(jobTitleId);
    //$("#btnViewMonth").click();
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function selectPage(index)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, index);
}

function viewWorktimeSheet(jobTitleId, year, month, pageIndex)
{
    $("input[name=tbxMonth]").val(month);
    $("input[name=tbxYear]").val(year);
    $("#hdfMonth").val(month);
    $("#hdfYear").val(year);
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Worktime/AjaxWorktime.aspx",
	    data : { 
		    "action" : "get_monthly_over_worktime_sheet", 
		    "jobTitleId" : jobTitleId,
		    "year" : year,
		    "month" : month,
		    "day" : 1,
		    "pageIndex" : pageIndex
	    }, 
	    success : function (r) {
	        if (r.status)
			{
			    var table = $("#worktimeTable");
			    var rows = table.find(".row");
			    rows.eq(0).find("div").eq(0).text(month + "/" + year);
			    for (var i=rows.length-1; i>0; i--)
			    {
			        rows.eq(i).remove();
			    }
			    for (var i=0; i<r.list.length; i++)
			    {
			        var htmlRow = "<div class='row'>";
			        htmlRow += "<div class='name-cell a2'>"+r.list[i].fullName+"</div>";
			        htmlRow += "<input name='agentId' value='"+r.list[i].agentId+"' type='hidden'>";
			        var k;
			        for (k=0; k<r.list[i].days.length; k++)
			        {
			            htmlRow += "<div class='cell tooltip'>";
			            htmlRow += r.list[i].days[k].total;
                        var tooltip = "";
                        if (r.list[i].days[k].timeExp != "")
                        {
                            tooltip = "<div class='tooltip-content'>";
                            tooltip += "<img class='callout' src='/image/layout/callout.gif'>";
                            tooltip += r.list[i].days[k].timeExp;
                            tooltip += "</div>";
                        }
                        htmlRow += tooltip;
                        
                        htmlRow += "</div>";
                    }
                    while (k < 31)
                    {
                        htmlRow += "<div class='cell tooltip'></div>";
                        k++;
                    }
                    htmlRow += "<div class='cell-total'>"+r.list[i].total+"</div>";
                    htmlRow += "</div>";
                    
                    table.append(htmlRow);
			    }
			    
			    var divPaging = $("#divPaging");
			    var paging = "";
			    for (var i=1; i<=r.totalPage; i++)
			    {
			        if (r.pageIndex == i)
			        {
			            paging += "<span>"+i+"</span>";
			        }
			        else
			        {
			            paging += "<a href='javascript:void(0);' onclick='selectPage("+i+");'>"+i+"</a>";
			        }
			    }
			    divPaging.html(paging);
			}
	    }
    })
    .always(function() {
	    unmask(area);
	});
}


function exportExcel()
{
    var li = $("a.jstree-clicked").closest("li");
    if (li.length  == 0)
    {
        return;
    }
    $("#hdfJobTitleId").val(li.attr("id"));
}



